package com.example.booktest.views;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.booktest.R;
import com.example.booktest.adapter.CatalogAdapter;
import com.example.booktest.entity.CatalogEntity;
import com.example.booktest.viewmodel.CatalogViewModel;

public class BookActivity extends AppCompatActivity {
    private CatalogViewModel viewModel;
    private RecyclerView recyclerView;
    private CatalogAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        viewModel = new ViewModelProvider(this).get(CatalogViewModel.class);
        recyclerView = findViewById(R.id.rvCatalog);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CatalogAdapter(); // Initialize with null, data will be set later
        recyclerView.setAdapter(adapter);

        viewModel.getCatalogData().observe(this, new Observer<CatalogEntity>() {
            @Override
            public void onChanged(CatalogEntity catalogEntity) {
                if (catalogEntity != null && catalogEntity.getResult() != null) {
                    adapter.setCatalogList(catalogEntity.getResult());
                }
            }
        });

        // Replace "your_api_key" with your actual API key
        viewModel.getGoodBooks("2c16970ce9651f0ce175217ff650f608");
    }
}